// 分类相关API调用 - 与后端CategoryController接口保持一致

import request from '@/utils/request'
import type { Category, CategoryTreeDTO, CategoryQueryParams, CategoryPageResponse } from '@/types/category'

// 新增分类（顶级或子级）
export const createCategory = (data: Category) => {
  return request.post<number>('/api/categories', data)
}

// 更新分类（按ID）
export const updateCategory = (id: number, data: Category) => {
  return request.put<boolean>(`/api/categories/${id}`, data)
}

// 分类详情
export const getCategoryDetail = (id: number) => {
  return request.get<Category>(`/api/categories/${id}`)
}

// 分页列表查询（支持名称/slug/父ID/层级/状态过滤）
export const getCategoryList = (params: CategoryQueryParams) => {
  return request.get<CategoryPageResponse>('/api/categories', { params })
}

// 查询直接子分类（parentId 为空则取顶级）
export const getCategoryChildren = (parentId?: number, status?: number) => {
  const params: any = {}
  if (parentId !== undefined) params.parentId = parentId
  if (status !== undefined) params.status = status
  return request.get<Category[]>('/api/categories/children', { params })
}

// 获取分类树（包含 children 嵌套结构，按 sort_order asc, id asc 排序）
export const getCategoryTree = (status?: number) => {
  const params = status !== undefined ? { status } : {}
  return request.get<CategoryTreeDTO[]>('/api/categories/tree', { params })
}

// 移动分类（更换父级）
export const moveCategory = (id: number, newParentId?: number) => {
  const params = newParentId !== undefined ? { newParentId } : {}
  return request.patch<boolean>(`/api/categories/${id}/move`, null, { params })
}

// 软删除分类（默认：存在子类则禁止删除）
export const deleteCategory = (id: number) => {
  return request.delete<boolean>(`/api/categories/${id}`)
} 